﻿#ifndef __CHATCONTROLLER__
#define __CHATCONTROLLER__

#include "BaseKit.h"
#include "network/WebSocket.h"

USING_NS_CC;

class ChatController:public CAViewController, public WebSocket::Delegate{
public:
	static ChatController* create(const char* name);
protected:
	ChatController(const char* name);
	virtual ~ChatController();
	void viewDidLoad();
	void viewDidUnload();
	void collectionViewDidSelectCellAtIndexPath(unsigned int section, unsigned int item);
	CACollectionViewCell* collectionCellAtIndex(const DSize& cellSize, unsigned int section, unsigned int item);
	DSize collectionViewCellSizeAtIndexPath(unsigned int section, unsigned int item);
	unsigned int numberOfItemsInSection(unsigned int section);
	void addNewMsg(std::string msg, bool isMe);
	void onOpen(WebSocket* ws);
	void onMessage(WebSocket* ws, const WebSocket::Data& data);
	void onClose(WebSocket* ws);
	void onError(WebSocket* ws, const WebSocket::ErrorCode& error);
private:
	const char* name;
	WebSocket* socket;
	CAScrollView* scrollView;
	CATextField* inputField;
	CAView* sendNode;
	CAAutoCollectionView* emojiView;
	CAVector<CAValue*> emojis;
	CAFont font;
	float curWidth=0.0f;
	float curHeight=40.0f;
	float maxWidth=0.0f;
};

#endif